package cn.tedu.boke.portal.mapper;

import cn.tedu.boke.portal.model.Permission;
import cn.tedu.boke.portal.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author tedu.cn
 * @since 2021-11-01
 */
@Repository
public interface UserMapper extends BaseMapper<User>
{
    @Select("select distinct p.id,p.name from user u left join user_role ur on u.id=ur.user_id left join role r on r.id=ur.role_id left join role_permission rp on r.id=rp.role_id left join permission p on p.id=rp.permission_id where u.id=#{id}")
    List<Permission> findUserPermissionsById(Integer id);

    //根据用户名查询用户对象
    @Select("select * from user where username=#{username}")
    User findUserByUsername(String username);

    @Delete("DELETE u,ur,p,pt,lik,com,col,vie FROM user u\n" +
            "LEFT JOIN user_role ur ON u.id=ur.user_id\n" +
            "LEFT JOIN post p ON u.id=p.user_id\n" +
            "LEFT JOIN post_tag pt ON p.id=pt.post_id\n" +
            "LEFT JOIN likenum lik ON u.id=lik.use_id\n" +
            "LEFT JOIN `comment` com ON u.id=com.user_id\n" +
            "LEFT JOIN collection col ON u.id=col.use_id\n" +
            "LEFT JOIN `view` vie ON u.id=vie.use_id\n" +
            "WHERE u.id=#{userId}")
    int cleanUserById(@Param("userId") Integer id);

    @Update("UPDATE user SET locked=1 WHERE id=#{userId}")
    int lockUserById(@Param("userId") Integer id);

    @Update("UPDATE user SET locked=0 WHERE id=#{userId}")
    int unLockUserById(@Param("userId") Integer id);
}
